 <#include "admin/common/include/html_head.html"> <#include
"admin/common/include/sysparam.html"> <#include
"admin/common/include/pagination.html">
<br>
<style>
 
.resParamTable{
	width: 80%; 
	margin: 20px;  
	padding: 10px;
}
.resParamTable th{
	 text-align: center; 
} 
.resParamTable tr {
	margin: 10px; 
	padding: 10px; 
}
.resParamTable td {
	text-align: center;
	padding: 10px; 
}
</style>
<#list resParamGroupList as group>
<table class="resParamTable" name="paramGroupTable">
	<thead>
		<tr>
			<th><br>参数组：<input class="m-wrap long span4" name="rootParamName" type="text" value="${group.rootResourceParam.paramName}"  placeholder="参数组名"/>
			排位：<input class="m-wrap long span2" name="rootParamRank" type="text" value="${group.rootResourceParam.rank}" placeholder="排位"/>
			
			 <a href='#' name="deleteParamGroup" onclick="$(this).parents('table')[0].remove();"><i
			class="icon-remove"></i></a></th>
			
		</tr>
	</thead>
	<tbody>
		<tr>
			<td>
				<table class="table table-bordered table-hover">
					<thead>
						<tr>
							<th width="30%">参数名称</th>
							<th width="30%">参数值</th>
							<th width="20%">排序</th>
							<th width="20%">操作</th>
						</tr>
					</thead>
					<tbody class="param-tbody">
						<#list group.childParamlist as childParam>
						<tr class="odd gradeX">
							<td><input class="m-wrap long span12" name="paramName" type="text"
								value="${childParam.paramName}" /></td>
							<td><input class="m-wrap long span12" name="paramValue" type="text"
								value="${childParam.paramValue}" /></td>
							<td><input class="m-wrap span6" name="rank" type="text"
								value="${childParam.rank}" /></td>
							<td><a href='#' name="edit_action"  onclick="$(this).parents('tr')[0].remove();"><i
									class="icon-remove"></i></a></td>
						</tr>
						</#list>
						<tr>
							<td colspan="4" >
								<button class="btn add-param">
									<i class="icon-plus"></i>&nbsp;&nbsp;新增参数
								</button>
							</td>
						</tr>
					</tbody>
				</table>
			</td>
		</tr>

	</tbody>
</table>


</#list>
 
<button class="btn  add-paramgroup">
	<i class="icon-plus"></i>&nbsp;&nbsp;新增参数组
</button>

<div class="form-actions">
		<button type="button" id="resparam_save_btn" class="btn blue">保存</button>  
</div>

<script>
	jQuery(document).ready(function() { 
		var resourceId = '${(resourceId)?if_exists}'; 
		bindAddParam();
		
		$(".add-paramgroup").bind("click", function() { 
			 var curObj = $(this);  
			 var trhtml =  common.getTemplateHtml('/admin/res/param/resparamgroup_pi.html'); 
		     $(trhtml).insertBefore(curObj); 
		     bindAddParam();
		});
		
		$("#resparam_save_btn").bind("click", function() { 
			var dataList = new Array();
			 $("table[name='paramGroupTable']").each(function(){
				 var rootParamNameObj =  $(this).find("input[name='rootParamName']")[0];
				 var rootParamRankObj =  $(this).find("input[name='rootParamRank']")[0];
				 var groupArray={};
				 groupArray["paramName"] = $(rootParamNameObj).val();
				 groupArray["rank"] = $(rootParamRankObj).val();
				 dataList.push(groupArray);
				 var length = $(this).find("input[name='paramName']").length; 
				 for(var i=0;i<length;i++){ 
					 var dataArray={}; 
					 var paramNameObj = $(this).find("input[name='paramName']")[i];
					 var paramValueObj = $(this).find("input[name='paramValue']")[i];
					 var rankObj = $(this).find("input[name='rank']")[i];
					 dataArray["rootParamName"] = $(rootParamNameObj).val();
					 dataArray["paramName"] = $(paramNameObj).val();
					 dataArray["paramValue"] = $(paramValueObj).val();
					 dataArray["rank"] = $(rankObj).val();
					 dataList.push(dataArray);
			      }
			 }); 
			 
			 if(!checkSaveParam(dataList))
					return;
			 var dataListJson = JSON.stringify(dataList);  
			 var param = "resparamList="+dataListJson;
			     param += "&resourceId="+resourceId;
			 ajax.syncJsonRequest({
 				url : '${contextPath}/Admin-ResParam-saveParamList.action',
 				param : param,
 				success : function(data) {
 					if (!data || !data.success) {
 						$('#errtip').html('保存失败！');
 						$('#errtip').show();
 					}
 					if (data.success) { 
 						common.alert({
 							content : '保存成功！',
 							closeFn : function() {  
 							}
 						});
 					}
 				}
 			}); 
		});
		
	});
	
	function checkSaveParam(dataList){  
		 for(var i=0;i<dataList.length;i++){ 
			 if(!dataList[i].paramName || !dataList[i].rank){ 
				 common.alert({
						content : '请正确填写完整的商品参数！',
						closeFn : function() {  
						}
					});
				return false;
			 }
		 }
		
		
		 for(var i=0;i<dataList.length;i++){
			 var count = 0;
			 for(var j=0;j<dataList.length;j++){
				 if(dataList[j].paramName ==dataList[i].paramName ){ 
					 if(++count > 1){
						 common.alert({
	 							content : '请不要出现重复的商品参数名称['+dataList[j].paramName+']！',
	 							closeFn : function() {  
	 							}
	 						});
					 return false;
					 }
				 }
			 }
		 }
		 return true;
	}
	
	function  bindAddParam(){
		$(".add-param").bind("click", function() {
			var tbody = $(this).parents(".param-tbody");
			var parentTr = $(this).parent().parent();  
			var trhtml =  common.getTemplateHtml('/admin/res/param/resparam_pi.html'); 
		    $(trhtml).insertBefore(parentTr); 
		});
	}
</script>


